package com.aa.base.log

import android.content.Context
import cn.thinkingdata.android.TDConfig
import cn.thinkingdata.android.ThinkingAnalyticsSDK
import cn.thinkingdata.android.ThinkingAnalyticsSDK.AutoTrackEventType
import com.aa.base.PlayerCheck
import com.aa.base.log.AppLog
import kotlinx.coroutines.joinAll
import org.json.JSONObject


/**
 * @author Afra55
 * @date 6/22/21
 * A smile is the best business card.
 */
object TAUtils {

    private const val pre = "clean_"

    private var instance: ThinkingAnalyticsSDK? = null
    fun init(context: Context) {
        try {// 在主线程中初始化 SDK
            instance = ThinkingAnalyticsSDK.sharedInstance(
                context,
                "3ac197d3e8d746ed9167c0fc40669e11",
                "https://analytics.socialcube.me/"
            )

            setThinkingDataAutoTrack(instance)



            if (PlayerCheck.isDebugMode) {
                ThinkingAnalyticsSDK.enableTrackLog(true)
            }

        } catch (e: Exception) {
            e.printStackTrace()
            AppLog.e(e, "taaaaaa")
        }

    }

    private fun setThinkingDataAutoTrack(thinkingAnalyticsSDK: ThinkingAnalyticsSDK?) {
        if (thinkingAnalyticsSDK != null) {
            val eventTypeList: MutableList<AutoTrackEventType> = ArrayList()
            //APP安装事件
            eventTypeList.add(AutoTrackEventType.APP_INSTALL)
            //APP启动事件
//            eventTypeList.add(AutoTrackEventType.APP_START)
            //APP关闭事件
//            eventTypeList.add(AutoTrackEventType.APP_END)
            //APP崩溃事件
            eventTypeList.add(AutoTrackEventType.APP_CRASH)
            thinkingAnalyticsSDK.enableAutoTrack(eventTypeList)
        }
    }

    fun setBasicProperty(vararg pair: Pair<String, String>) {
        if (pair.isNotEmpty()) {
            val jsonObject = JSONObject()
            for (i in pair) {
                jsonObject.put(i.first, i.second)
            }
            instance?.superProperties = jsonObject
        }
    }

    /**
     * 基本事件
     */
    @SafeVarargs
    fun trackEvent(eventName: String, vararg pair: Pair<String, String>) {
        if (instance == null) {
            PlayerCheck.context?.let { init(it) }
        }
        AppLog.i(eventName, "taaaaaa")
        try {
            val eventName1 = pre + eventName
            if (pair.isNotEmpty()) {
                val jsonObject = JSONObject()
                for (i in pair) {
                    jsonObject.put(i.first, i.second)
                }
                instance?.track(eventName1, jsonObject)
            } else {
                instance?.track(eventName1)
            }
        } catch (e: Exception) {
            AppLog.e(e, "taaaaaa")
        }
    }

    /**
     * 需要计时某个事件，需要先调用该方法开始计时，一段事件后然后再上报事件，这时事件会自己带上 duration 参数
     */
    fun trackTimeEvent(eventName: String) {
        try {
            instance?.timeEvent(pre + eventName)
        } catch (e: Exception) {
        }
    }

    @SafeVarargs
    fun trackShowEvent(eventName: String, vararg pair: Pair<String, String>) {
        trackEvent("show_$eventName", *pair)
    }

    @SafeVarargs
    fun trackPageBeginEvent(eventName: String, vararg pair: Pair<String, String>) {
        trackEvent("pvbegin_$eventName", *pair)
        trackPageEndTimeEvent(eventName)
    }

    @SafeVarargs
    fun trackPageEndEvent(eventName: String, vararg pair: Pair<String, String>) {
        trackEvent("pvend_$eventName", *pair)
    }

    @SafeVarargs
    fun trackClickEvent(eventName: String, who:String, vararg pair: Pair<String, String>) {
        trackEvent("click_${eventName}__$who", *pair)
    }


    fun trackPageEndTimeEvent(eventName: String) {
        trackTimeEvent("pvend_$eventName")
    }

    fun trackAdBanner(from:String, network:String) {
        trackEvent("IM_Banner", "from" to from, "network" to network)
    }

    fun trackAdInterPageShouldShow(from:String, network:String) {
        trackEvent("interpage_should_show", "from" to from, "network" to network)
    }
    fun trackAdInterPageRealShow(from:String, network:String) {
        trackEvent("IM_Interpage", "from" to from, "network" to network)
    }
    fun trackAdInterstitialShouldShow(from:String, network:String) {
        trackEvent("interstitial_should_show", "from" to from, "network" to network)
    }
    fun trackAdInterstitialRealShow(from:String, network:String) {
        trackEvent("IM_Interstitial", "from" to from, "network" to network)
    }
    fun trackAdRewardShouldShow(from:String, network:String) {
        trackEvent("reward_should_show", "from" to from, "network" to network)
    }
    fun trackAdRewardRealShow(from:String, network:String) {
        trackEvent("IM_Reward", "from" to from, "network" to network)
    }

    fun trackAdNativeShouldShow(from:String, network:String) {
        trackEvent("native_should_show", "from" to from, "network" to network)
    }
    fun trackAdNativeRealShow(from:String, network:String) {
        trackEvent("IM_Native", "from" to from, "network" to network)
    }

    fun trackAdSplashShouldShow(from:String, network:String) {
        trackEvent("splash_should_show", "from" to from, "network" to network)
    }
    fun trackAdSplashRealShow(from:String, network:String) {
        trackEvent("IM_Splash", "from" to from, "network" to network)
    }
    fun trackAdScreenShouldShow(from:String, network:String) {
        trackEvent("screen_ad_should_show", "from" to from, "network" to network)
    }
    fun trackAdScreenRealShow(from:String, network:String) {
        trackEvent("IM_Screen_ad", "from" to from, "network" to network)
    }
}